using System;
using System.Text.RegularExpressions;
using System.Web;

using Swaf;
using Swaf.BizObj;

public class SyrinxGlobal : Swaf.Gui.AspNet2.AspNetGlobal
{
	public string AppConfigFileName { get { return @"\configFiles\StarterWebApplication.xml"; } }

	protected static Regex s_rss = new Regex(@"(.*?)/rss/([^/]+?)\.aspx$", RegexOptions.IgnoreCase);
	protected static Regex s_articleOnly = new Regex(@"(.*?)/articles/([^/]+?)\.aspx$", RegexOptions.IgnoreCase);
	protected static Regex s_articleWithPid = new Regex(@"(.*?)/articles/([^_]+?)_([^/]*?)\.aspx$", RegexOptions.IgnoreCase);
	protected static Regex s_productOnly = new Regex(@"(.*?)/products/([^/]+?)\.aspx$", RegexOptions.IgnoreCase);
	protected static Regex s_productWithPid = new Regex(@"(.*?)/products/([^/]+?)_([^/]+?)\.aspx$", RegexOptions.IgnoreCase);
	protected static Regex s_yellowPages = new Regex(@"(.*?)/YellowPages/([^/]+?)\.aspx$", RegexOptions.IgnoreCase);
	protected static Regex s_blogPages = new Regex(@"(.*?)/Blogs/([^/]+?)\.aspx$", RegexOptions.IgnoreCase);
	protected static Regex s_companyNameUrl = new Regex(@"[^.]*\.([^.]+)\.", RegexOptions.IgnoreCase);

	public void Application_AuthorizeRequest(object sender, EventArgs e)
	{
		HttpContext incoming = HttpContext.Current;
		string oldpath = incoming.Request.Path;
		string newPath = oldpath;
		string companyId = "";
		IResults rc = Swaf.Application.globalApp.ruleMgr.execute("getCompanyFromDomainName", incoming.Request.Url);
		IBizObj company = rc.CallResults as IBizObj;

		if (company != null && !Cmn.IsEmpty(company.get("settings.sitePath")) &&
			oldpath != "/AjaxRequestHandler.aspx" && oldpath != "/NitiboLiveStreamHandler.aspx" &&
			oldpath.ToLower().EndsWith(".aspx"))
		{
			companyId = company.get("settings.sitePath").ToString();
			base.m_usingCompanyUrl = true;
		}

		// Regular expressions to grab the page id from the pageX.aspx 
		Match m;
		if ((m = s_articleWithPid.Match(oldpath)).Success)
			newPath = string.Format("{0}/std/ArticleView.aspx?gArt1pid={1}&gArt1id={2}", !Cmn.IsEmpty(companyId) ? companyId : m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
		else if ((m = s_articleOnly.Match(oldpath)).Success)
			newPath = string.Format("{0}/std/ArticleView.aspx?gArt1pid={1}&gArt1id={1}", !Cmn.IsEmpty(companyId) ? companyId : m.Groups[1].Value, m.Groups[2].Value);
		else if ((m = s_productOnly.Match(oldpath)).Success)
			newPath = string.Format("{0}/std/ProductView.aspx?pArt1pid={1}&pArt1id={1}", !Cmn.IsEmpty(companyId) ? companyId : m.Groups[1].Value, m.Groups[2].Value);
		else if ((m = s_productWithPid.Match(oldpath)).Success)
			newPath = string.Format("{0}/std/ProductView.aspx?pArt1pid={1}&pArt1id={2}", !Cmn.IsEmpty(companyId) ? companyId : m.Groups[1].Value, m.Groups[2].Value, m.Groups[3].Value);
		else if ((m = s_yellowPages.Match(oldpath)).Success)
			newPath = string.Format("{0}/std/YellowPages.aspx?gArt2id={1}", !Cmn.IsEmpty(companyId) ? companyId : m.Groups[1].Value, m.Groups[2].Value);
		else if (!base.m_usingCompanyUrl && string.Compare(oldpath, "/yellowpages.aspx", true) == 0)
			newPath = "/std/YellowPages.aspx";
		else if ((m = s_blogPages.Match(oldpath)).Success)
			newPath = string.Format("{0}/std/Blog.aspx?bArtid={1}", !Cmn.IsEmpty(companyId) ? companyId : m.Groups[1].Value, m.Groups[2].Value);
		else if ((m = s_rss.Match(oldpath)).Success)
			newPath = string.Format("{0}/std/Rss.aspx?id={1}", m.Groups[1].Value, m.Groups[2].Value);
		else if (!Cmn.IsEmpty(companyId) && !oldpath.StartsWith(companyId))
			newPath = companyId + oldpath;

		incoming.RewritePath(newPath, true);
	}
}
